<?php
/**
 * 刘杨
 */
namespace frontend\controllers;
use Yii;
use backend\models\Position;
use backend\models\Classify;
use yii\data\Pagination;
use frontend\models\UserPosition;
use frontend\models\User_details;
use frontend\models\Stb_area;;
class PositionController extends \yii\web\Controller
{
	public $layout = 'min_business';
	//判断是否登录
	public function init(){
		$session = Yii::$app->session;
    	$business_id = $session['business']['business_id'];
    	if(empty($business_id)){
    		$this->redirect('?w=businesslogin/index');
    	}
	}
    //展示商家招聘信息
    public function actionPosition_list()
    {
    	$session = Yii::$app->session;
        //取出商家用户登录ID
    	$business_id = $session['business']['business_id'];
    	$position = Position::find();
        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $position->count(),
        ]);
        $data = $position->offset($pagination->offset)
        ->limit($pagination->limit)
        ->where(['account_id'=>$business_id])
        ->orderBy('id desc')
        ->asArray()
        ->all();
        $Classify = Classify::find();
        foreach ($data as $k => $v) {
            $Classify = Classify::findOne($v['classify_id']);
            $data[$k]['classify_name'] = $Classify->classify_name;
        }
        return $this->render('position_list',['data'=>$data,'pagination'=>$pagination]);
    }
    //发布招聘信息
    public function actionPosition_add(){
        if($data = Yii::$app->request->post())
        {   
            $position = new Position;
            $position->pos_name = $data['pos_name'];
            $position->pos_salary = $data['pos_salary'];
            $position->settlement = $data['settlement'];
            $position->pos_sex = $data['pos_sex'];
            $position->classify_id = $data['classify_id'];
            $position->pos_interview = $data['pos_interview'];
            $position->pos_num = $data['pos_num'];
            $position->pos_introduce = str_replace("\n",'<br>',$data['pos_introduce']);
            $position->pos_show = $data['pos_show'];
            $position->addtime = time();
            $position->account_id = Yii::$app->session['business']['business_id'];
            $area_id = implode(',',$data['area_name']);
            $area_name = Stb_area::find()->select('area_name')->where("id in ($area_id)")->asArray()->all();
            
            foreach ($area_name as $k => $v) {
                $area[] = $v['area_name'];
            }
            $position->pos_area = implode(',',$area);
            if($position->insert())
            {
                $this->redirect('?w=position/position_list');
            }
        }
        else
        {
            $data = Classify::find()->asArray()->all();
            $area = Stb_area::find()->where(['parent_id'=>0])->all();
            $classify = $this->child($data);
            return $this->render('position_add',['classify'=>$classify,'area'=>$area]);
        }
        
    }
    //地区选择
    public function actionPosition_area()
    {
        $id = Yii::$app->request->get('id');
        $area = Stb_area::find()->select('id,area_name')->where(['parent_id'=>$id])->asArray()->all();
        if($area)
        {
            exit(json_encode(array('error'=>1,'area'=>$area)));
        }else{
            exit(json_encode(array('error'=>0)));
        }
    }
    //招聘消息
    public function actionPosition_news(){
        $session = Yii::$app->session;
        $business_id = $session['business']['business_id'];
        $UserPosition = UserPosition::find();
        $position = Position::find();
        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $position->count(),
        ]);
        $data = $position->offset($pagination->offset)
        ->limit($pagination->limit)
        ->where(['account_id'=>$business_id])
        ->select('id,pos_name,addtime,pos_num')
        ->orderBy('id desc')
        ->asArray()
        ->all();
        foreach ($data as $k => $v) {
            //已招人数
            $data[$k]['hasDr'] = $UserPosition->where(['position_id'=>$v['id'],'pos_state'=>1])->count();
            //未审核人数
            $data[$k]['notMove'] = $UserPosition->where(['position_id'=>$v['id'],'pos_state'=>0])->count();
        }
        return $this->render('position_news',['data'=>$data,'pagination'=>$pagination]);
    }
    //展示商家招聘用户消息
    public function actionPosition_user()
    {
        $position_id = Yii::$app->request->get('id');
        $UserPosition = UserPosition::find();
        $userpos = $UserPosition->select('user_id')->where(['position_id'=>$position_id,'pos_state'=>0])->asArray()->all();
        foreach ($userpos as $k => $v) {
            $user_id[] = $v['user_id'];
        }
        if(empty($user_id))
        {
            $this->redirect(['position_news']);
        }
        $User_details = User_details::find();
        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $User_details->count(),
        ]);
        $data = $User_details->offset($pagination->offset)
        ->limit($pagination->limit)
        ->where("user_id in (".implode(',',$user_id).")")
        ->asArray()
        ->all();
        return $this->render('position_user',['data'=>$data,'pagination'=>$pagination]);
    }
    //是否录用
    public function actionPosition_upstate()
    {
        $user_id = Yii::$app->request->get('id');
        $pos_state = Yii::$app->request->get('state');
        $save = Yii::$app->db->createCommand()->update('user_position',
            ['pos_state'=>$pos_state],"user_id=$user_id")->execute();
        if($save)
        {
            exit(json_encode(array('error'=>1,'msg'=>'当前操作成功')));
        }else{
            exit(json_encode(array('error'=>1,'msg'=>'当前操作失败')));
        }
    }
    //分类
    function child($data,$pid='0')
    {
        $arr = array();
        foreach ($data as $k => $v) {
            if($v['classify_action']==$pid)
            {
                $child = $this->child($data,$v['id']);
                if($child)
                {
                    $v['child'] = $child;
                }
                $arr[] = $v;
            }
        }
        return $arr;
    }
}
